import app from '../../app.json';
var count = 0;//计时器开始时，给一个全局变量用于减少时间差（以秒为单位，每次增加1）
function timestampToTime(timestamp) {
    const dateObj = new Date(+timestamp) // ps, 必须是数字类型，不能是字符串, +运算符把字符串转化为数字，更兼容
    const year = dateObj.getFullYear() // 获取年，
    const month = dateObj.getMonth() + 1 // 获取月，必须要加1，因为月份是从0开始计算的
    const date = dateObj.getDate() // 获取日，记得区分getDay()方法是获取星期几的。
    const hours = pad(dateObj.getHours())  // 获取时, pad函数用来补0
    const minutes = pad(dateObj.getMinutes()) // 获取分
    const seconds = pad(dateObj.getSeconds()) // 获取秒
    return year + '-' + (month < 10 ? "0" + month : month) + '-' + (date < 10 ? '0' + date : date) + ' ' + hours + ':' + minutes + ':' + seconds
}
function timestampToWeekday(time) {
    const dateObj = ConvertStrToDate(time) // ps, 必须是数字类型，不能是字符串, +运算符把字符串转化为数字，更兼容
    const year = dateObj.getFullYear() // 获取年，
    const month = dateObj.getMonth() + 1 // 获取月，必须要加1，因为月份是从0开始计算的
    const date = dateObj.getDate() // 获取日，记得区分getDay()方法是获取星期几的。
    const hours = pad(dateObj.getHours())  // 获取时, pad函数用来补0
    const minutes = pad(dateObj.getMinutes()) // 获取分
    const seconds = pad(dateObj.getSeconds()) // 获取秒
    let ntime = year + '-' + (month < 10 ? "0" + month : month) + '-' + (date < 10 ? '0' + date : date);
    var array = ntime.split('-');
    var ndate = new Date(array[0], parseInt(array[1] - 1), array[2]);
    var weekArray = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    return weekArray[ndate.getDay()];
}
//把字符串日期转为日期
function ConvertStrToDate(datetimeStr) {
    var mydateint = Date.parse(datetimeStr);//数值格式的时间
    if (!isNaN(mydateint)) {
        var mydate = new Date(mydateint);
        return mydate;
    }
    var mydate = new Date(datetimeStr);//字符串格式时间
    var monthstr = mydate.getMonth() + 1;
    if (!isNaN(monthstr)) {//转化成功
        return mydate;
    }//字符串格式时间转化失败
    var dateParts = datetimeStr.split(" ");
    var dateToday = new Date();
    var year = dateToday.getFullYear();
    var month = dateToday.getMonth();
    var day = dateToday.getDate();
    if (dateParts.length >= 1) {
        var dataPart = dateParts[0].split("-");//yyyy-mm-dd  格式时间             
        if (dataPart.length == 1) {
            dataPart = dateParts[0].split("/");//yyyy/mm/dd格式时间
        }
        if (dataPart.length == 3) {
            year = Math.floor(dataPart[0]);
            month = Math.floor(dataPart[1]) - 1;
            day = Math.floor(dataPart[2]);
        }
    }
    if (dateParts.length == 2) {//hh:mm:ss格式时间
        var timePart = dateParts[1].split(":");//hh:mm:ss格式时间
        if (timePart.length == 3) {
            var hour = Math.floor(timePart[0]);
            var minute = Math.floor(timePart[1]);
            var second = Math.floor(timePart[2]);
            return new Date(year, month, day, hour, minute, second);
        }
    }
    else {
        return new Date(year, month, day);
    }
}
function pad(str) {
    return +str >= 10 ? str : '0' + str
}
function compareDate(s1) {
    s1 = s1.replace(/-/g, '/') // 把所有-转化成/
    s1 = new Date(s1).getTime();
    let s2 = new Date().getTime();
    return s1 > s2;
}
function deepCopy(obj) {
    // 只拷贝对象
    if (typeof obj !== 'object') return;
    // 根据obj的类型判断是新建一个数组还是一个对象
    var newObj = obj instanceof Array ? [] : {};
    for (var key in obj) {
        // 遍历obj,并且判断是obj的属性才拷贝
        if (obj.hasOwnProperty(key)) {
            // 判断属性值的类型，如果是对象递归调用深拷贝
            newObj[key] = typeof obj[key] === 'object' ? deepCopy(obj[key]) : obj[key];
        }
    }
    return newObj;
}


function plusXing(str, frontLen, endLen) {
    var arr = str.split("");
    for (let i = frontLen; i <= endLen; i++) {
        arr[i] = '*';
    }
    return arr.join("");
}
function checkbeishuInput(beishuInput) {
    if (beishuInput == '' || Number(beishuInput) == 0 || isNaN(Number(beishuInput))) {
        beishuInput = "";
    } else {
        if (beishuInput > app.maxBeiShu) {
            beishuInput = app.maxBeiShu;
        }
        beishuInput = parseInt(beishuInput).toString()
    }
    return beishuInput;
}
function getRandomNumber(m, n) {
    return Math.floor(Math.random()*(m - n) + n);
}
function htmlEncodeByRegExp (str){  
    var temp = "";
    if(str.length == 0) return "";
    temp = str.replace(/&/g,"&amp;");
    temp = temp.replace(/</g,"&lt;");
    temp = temp.replace(/>/g,"&gt;");
    temp = temp.replace(/\s/g,"&nbsp;");
    temp = temp.replace(/\'/g,"&#39;");
    temp = temp.replace(/\"/g,"&quot;");
    return temp;
}
function htmlDecodeByRegExp (str){  
    var temp = "";
    if(str.length == 0) return "";
    temp = str.replace(/&amp;/g,"&");
    temp = temp.replace(/&lt;/g,"<");
    temp = temp.replace(/&gt;/g,">");
    temp = temp.replace(/&nbsp;/g," ");
    temp = temp.replace(/&#39;/g,"\'");
    temp = temp.replace(/&quot;/g,"\"");
    return temp;  
}
function updategetYMDhms(start, end) {
    count += 1;//该方法执行几次，count相应++
    var date1 = new Date(start);
    var date2 = new Date(end);
    var s1 = date1.getTime();
    var s2 = date2.getTime();//毫秒为单位
    var total = (s2 - s1) / 1000 - count;//每执行一次，减少时间差-1
    var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
    var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
    var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
    var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
    var min = parseInt(afterHour / 60); //计算整数分
    var afterMin = parseInt(total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    return {
        day:day,
        hour:hour,
        min:min,
        afterMin:afterMin
    }      

}
function updatecountdown(total){
    var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
    var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
    var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
    var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
    var min = parseInt(afterHour / 60); //计算整数分
    var afterMin = parseInt(total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    return {
        day:day,
        hour:hour,
        min:min,
        afterMin:afterMin
    }
}
 // 随机的生成[begin, end] 范围内的数据
 function random_int(begin, end) {
    var num = begin + Math.random() * (end - begin + 1);
    num = Math.floor(num);
    if (num > end) {
        num = end;
    }
    return num;
}

module.exports = {
    pad:pad,
    timestampToTime: timestampToTime,
    compareDate: compareDate,
    deepCopy: deepCopy,
    plusXing: plusXing,
    checkbeishuInput: checkbeishuInput,
    timestampToWeekday: timestampToWeekday,
    getRandomNumber:getRandomNumber,
    htmlEncodeByRegExp:htmlEncodeByRegExp,
    htmlDecodeByRegExp:htmlDecodeByRegExp,
    updategetYMDhms:updategetYMDhms,
    updatecountdown:updatecountdown,
    random_int:random_int,
}